package com.ddxz.middleground.master.controller;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.dept.DeptInfo;
import com.ddxz.middleground.master.service.DepartmentService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/26
 */

@Api(tags = "部门管理")
@RestController
public class DepartmentController {
    @ApiOperation(value = "部门检索", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/dept/tree", method = RequestMethod.GET)
    public DdxzResponse deptList(@DdxzRequestHeader MasterHeader header,
                                 @RequestParam(name = "keyword", required = false, defaultValue = "") String keyword) {
        return service.deptList(header, keyword);
    }

    @ApiOperation(value = "部门详情", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/dept/{deptId}/detail", method = RequestMethod.GET)
    public DdxzResponse deptDetail(@DdxzRequestHeader MasterHeader header,
                                   @PathVariable(name = "deptId") String deptId) {
        return service.deptDetail(deptId, header);
    }

    @ApiOperation(value = "部门保存", consumes = "application/json", produces = "application/json")
    @RequestMapping(value = "/dept/save", method = RequestMethod.POST)
    public DdxzResponse deptSave(@DdxzRequestHeader MasterHeader header,
                                 @RequestBody DeptInfo deptReq) {
        return service.deptSave(header, deptReq);
    }

    @ApiOperation(value = "部门删除", consumes = "application/json", produces = "application/json",
            notes = "只能删除空部门，如果部门下存在子部门或用户则不允许进行删除操作")
    @RequestMapping(value = "/dept/{deptId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse deptDelete(@DdxzRequestHeader MasterHeader header,
                                   @PathVariable(name = "deptId") String deptId) {
        return service.deptDelete(header, deptId);
    }

    @ApiOperation(value = "部门移动", consumes = "application/json", produces = "application/json",
            notes = "如果当前部门排序为1则不能上移，如果部门排序为最大则不能下移；" +
                    "如果当前部门上方没有兄弟部门则不能下移，如果上级部门标识为-1则不能上移." +
                    "direction: 1 上移；2 下移； 3 升级； 4 降级")
    @RequestMapping(value = "/dept/{deptId}/move", method = RequestMethod.PUT)
    public DdxzResponse deptMove(@DdxzRequestHeader MasterHeader header,
                                 @PathVariable("deptId") String deptId,
                                 @RequestParam(name = "direction") Integer direction) {
        return service.deptMove(deptId, direction, header);
    }

    @Autowired
    private DepartmentService service;
}
